package com.csu.tour.controller;

import com.csu.tour.common.CommonResponse;
import com.csu.tour.entity.Emergency;
import com.csu.tour.entity.User;
import com.csu.tour.service.UserService;
import com.csu.tour.vo.UserVO;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Validated
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/tourist/login")
    @ResponseBody
    public CommonResponse<UserVO> login(@RequestBody UserVO userVO,
                                        HttpSession session) {
        if(userVO.getUsername().isEmpty()) {
            System.out.println("用户名为空");
            return CommonResponse.createForError(1,"请输入用户名");
        }
        if(userVO.getPassword().isEmpty()) {
            System.out.println("密码为空");
            return CommonResponse.createForError(1, "请输入密码");
        }
        UserVO userVo = userService.getUserByNameAndPassword(userVO.getUsername(), userVO.getPassword());
        if(userVo == null) {
            return CommonResponse.createForError(1, "用户名或密码错误");
        }else{
            session.setAttribute("user", userVO);
            return CommonResponse.createForSuccess("登录成功", userVO);
        }
    }

    @PostMapping("/admin/login")
    @ResponseBody
    public CommonResponse<UserVO> login1(@RequestBody UserVO userVO,
                                        HttpSession session){
        if(userVO.getUsername().isEmpty()) {
            System.out.println("用户名为空");
            return CommonResponse.createForError(1,"请输入用户名");
        }
        if(userVO.getPassword().isEmpty()) {
            System.out.println("密码为空");
            return CommonResponse.createForError(1, "请输入密码");
        }
        UserVO userVo = userService.getUserByNamePasswordRole(userVO.getUsername(), userVO.getPassword(),userVO.getRole());
        if(userVo == null) {
            return CommonResponse.createForError(1, "用户名或密码错误");
        }else{
            session.setAttribute("user", userVO);
            return CommonResponse.createForSuccess("登录成功", userVO);
        }
    }

    @PostMapping("/tourist/register")
    @ResponseBody
    public CommonResponse<UserVO> register(@RequestBody UserVO userVO, HttpSession session){
        userVO.setRole("游客");
        String username = userVO.getUsername();
        String password = userVO.getPassword();
        String role = userVO.getRole();
        if(userService.getUserByNamePasswordRole(username, password, role) != null) {
            return CommonResponse.createForError(1, "用户已存在");
        }
        userService.addUser(userVO);
        userVO.setId(userService.getUserByNamePasswordRole(username, password, role).getId());
        session.setAttribute("user", userVO);
        return CommonResponse.createForSuccess("注册成功", userVO);
    }

    @DeleteMapping("/delete")
    public CommonResponse<User> deleteUser(@RequestBody User user) {
        if (userService.deleteUser(user.getId())==1) {
            String msg = "成功";
            return CommonResponse.createForSuccess(msg, null);
        } else {
            return CommonResponse.createForError(500, "失败");
        }

    }

    @PutMapping("/update")
    public CommonResponse<User> updateUser(@RequestBody User user) {
        if (userService.updateUser(user)==1) {
            String msg = "成功";
            return CommonResponse.createForSuccess(msg,user);
        } else {
            return CommonResponse.createForError(500, "失败");
        }

    }

    @GetMapping("/select")
    public CommonResponse<List<User>> selectUser() {
        List<User> userList= userService.selectTourists("游客") ;
            boolean temp = userList.isEmpty();
            //数据库查询成功但数据库中没有任何数据
            if (temp) {
                String msg="当前没有游客";
                return CommonResponse.createForSuccessMessage(msg);
            }else {
                // 创建成功响应，包含数据
                String msg="数据库查询成功";
                return CommonResponse.createForSuccess(userList);
            }
    }
}


